package segmenter;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import edu.stanford.nlp.ie.crf.CRFClassifier;
import edu.stanford.nlp.ling.CoreLabel;


/** This is a very simple demo of calling the Chinese Word Segmenter
 *  programmatically.  It assumes an input file in UTF8.
 *  <p/>
 *  <code>
 *  Usage: java -mx1g -cp seg.jar SegDemo fileName
 *  </code>
 *  This will run correctly in the distribution home directory.  To
 *  run in general, the properties for where to find dictionaries or
 *  normalizations have to be set.
 *
 *  @author Christopher Manning
 */

public class Segmenter {
	
	public static void main(String[] args) throws IOException {
		Segmenter segmenter = Segmenter.getInstance();
		segmenter.getClassifier().classifyAndWriteAnswers(PATH_PREFIX + "input.txt");
	}
	
	static final private String PATH_PREFIX =
			"D:/DH/software/stanford-segmenter-2012-07-09/";
	
	private CRFClassifier<CoreLabel> classifier;
	static private Segmenter instance = null;
	
	private Segmenter() {
		
		Properties props = new Properties();
		props.setProperty("sighanCorporaDict", PATH_PREFIX + "data");
		props.setProperty("serDictionary", 
				PATH_PREFIX + "data/dict-chris6.ser.gz," + 
				"usr.dict.txt");
		props.setProperty("inputEncoding", "UTF-8");
//		props.setProperty("inputEncoding", "GBK");
		props.setProperty("sighanPostProcessing", "true");

		
		classifier = new CRFClassifier<CoreLabel>(props);
		classifier.loadClassifierNoExceptions(PATH_PREFIX + "data/ctb.gz", props);
//		flags must be re-set after data is loaded
		
		classifier.flags.setProperties(props);
		
		// 用来把结果写到文件中
//		classifier.classifyAndWriteAnswers(args[0]);
	}
	
	static public Segmenter getInstance() {
		return (instance == null) ? (instance = new Segmenter()) : instance;
	}
	
	public CRFClassifier<CoreLabel> getClassifier() {
		return classifier;
	}
	
	public List<String> segment(String segmentString) {
		return classifier.segmentString(segmentString);
	}
	
}

